#!/bin/bash
# /etc/ImageMagick-6/policy.xml needs munging
# https://askubuntu.com/questions/1127260/imagemagick-convert-not-allowed
# pip install pysstv

FOO=/tmp/$$.txt
FOOIMAGE=/tmp/$$.png
FOOWAV=$1
TIMEL=`date +"%A %e %B %Y%n                %l:%M %p"`

declare -a TRAILTXT=(
"Email contact: VK2CJB@WIA.ORG.AU"
"Hi there!  (^_^)"
"QTH Ellalong, 6 Watts."
"Generated with Linux, Bash, and Python."
)

i=$(($RANDOM % ${#TRAILTXT[@]}))
TRAILER=${TRAILTXT[$i]}

cat >$FOO <<EOF
*** SSTV  TEST  BROADCAST  de VK2CJB *** 

         $TIMEL
             
----------- News & schedules -----------

  * 24:00  VK2CJB SSTV Net       146.575
MWF  9:30  VK2ATZ Breakfast Net  146.775
Mon 19:30  VK2AWX Weekly News    146.900
Wed 19:00  VK2RGL Weekly Net     147.100
Wed 19:30  VK2AWX Wed Night Chat 146.900
Thu 19:30  VK2ATZ Lets Stay Safe 146.775
Sun  6:00  VK2ATZ Stoned Crows     3.595
Sun  9:00  VK2ATZ News           146.975
Sun 10:00  WIA+ARNSW News        146.975

----------------------------------------
$TRAILER
EOF
tlen=`cat $FOO|wc -l`
if (( tlen != 19 )); then echo "Message != 19 lines"; exit; fi

declare -a COLS=(
"darkblue" "darkred" "darkgreen" "DarkTurquoise"
"black" "gray20" "gray40" "darkviolet" "darkcyan"
)
i=$(($RANDOM % ${#COLS[@]}))
COLOUR=${COLS[$i]}

declare -a FONTS=( "Pet-Me" "Pet-Me-64" )
i=$(($RANDOM % ${#FONTS[@]}))
FONT=${FONTS[$i]}

convert -size 320x256 \
	-font $FONT -pointsize 8 -interline-spacing 4 \
	xc:$COLOUR -fill white \
	-annotate +0+16 "@$FOO" \
	$FOOIMAGE

python -m pysstv --mode ScottieS1 --keep-aspect-ratio --resize \
       --resample bicubic $FOOIMAGE $FOOWAV

#rm -f $FOOIMAGE $FOO

exit
